using Net35.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Net35.Xrm.Sdk.Linq
{
	internal static class MemberInfoExtensions
	{
		private struct MemberInfoAttributeType
		{
			public MemberInfo MemberInfo;
			public Type Type;
		}
		private static readonly ConcurrentDictionary<MemberInfoExtensions.MemberInfoAttributeType, IEnumerable<object>> _memberInfoToAttributesLookup = new ConcurrentDictionary<MemberInfoExtensions.MemberInfoAttributeType, IEnumerable<object>>();
		public static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo info) where T : Attribute
		{
			MemberInfoExtensions.MemberInfoAttributeType key = new MemberInfoExtensions.MemberInfoAttributeType
			{
				MemberInfo = info,
				Type = typeof(T)
			};
			IEnumerable<object> orAdd = MemberInfoExtensions._memberInfoToAttributesLookup.GetOrAdd(key, (MemberInfoExtensions.MemberInfoAttributeType _) => info.GetCustomAttributes(typeof(T), true));
			return orAdd.Cast<T>();
		}
		public static T GetFirstOrDefaultCustomAttribute<T>(this MemberInfo info) where T : Attribute
		{
			return info.GetCustomAttributes<T>().FirstOrDefault<T>();
		}
		public static string GetLogicalName(this MemberInfo property)
		{
			AttributeLogicalNameAttribute firstOrDefaultCustomAttribute = property.GetFirstOrDefaultCustomAttribute<AttributeLogicalNameAttribute>();
			if (firstOrDefaultCustomAttribute != null)
			{
				return firstOrDefaultCustomAttribute.LogicalName;
			}
			EntityLogicalNameAttribute firstOrDefaultCustomAttribute2 = property.GetFirstOrDefaultCustomAttribute<EntityLogicalNameAttribute>();
			if (firstOrDefaultCustomAttribute2 != null)
			{
				return firstOrDefaultCustomAttribute2.LogicalName;
			}
			RelationshipSchemaNameAttribute firstOrDefaultCustomAttribute3 = property.GetFirstOrDefaultCustomAttribute<RelationshipSchemaNameAttribute>();
			if (firstOrDefaultCustomAttribute3 != null)
			{
				return firstOrDefaultCustomAttribute3.SchemaName;
			}
			return null;
		}
	}
}
